package com.xu.controller;

import com.xu.mapper.VouchersMapper;
import com.xu.pojo.Vouchers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
public class VouchersController {

    @Autowired
    VouchersMapper vouchersMapper;

    @RequestMapping("/toVourchers")
    public String toVourchers(Model model)
    {
        List<Vouchers> vouchers = vouchersMapper.queryAllVouchers();
        model.addAttribute("vouchers", vouchers);
        return "/admin/vouchers/vouchers";
    }

    @RequestMapping("toVouchersAdd")
    public String toVouchersAdd()
    {
        return "/admin/vouchers/vouchers-add";
    }

    @RequestMapping("/vouchersAdd")
    public String vouchersAdd(Vouchers vouchers)
    {
        vouchersMapper.addVouchers(vouchers);
        return "redirect:/toVourchers";
    }

    @RequestMapping("/vouchersDel/{id}")
    public String vouchersDel(@PathVariable("id")int id)
    {
        vouchersMapper.delVouchers(id);
        return "redirect:/toVourchers";
    }

    @RequestMapping("/toVouchersUpd/{id}")
    public String toVouchersUpd(@PathVariable("id")int id,Model model)
    {
        Vouchers vouchers = vouchersMapper.queryVouchersById(id);
        model.addAttribute("vouchers", vouchers);
        return "admin/vouchers/vouchers-upd";
    }

    @RequestMapping("/vouchersUpd")
    public String vouchersUpd(Vouchers vouchers)
    {
        vouchersMapper.updVouchers(vouchers);
        return "redirect:/toVourchers";
    }

}
